/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.statweb.action;

import cn.ac.iie.di.wx.statweb.utils.DateUtil;
import cn.ac.iie.di.wx.statweb.utils.PicCopy;
import cn.ac.iie.di.wx.statweb.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Administrator
 */
public class UserAccessAction {

    private String trunk;
    static Logger logger = Logger.getLogger(UserAccessAction.class.getName());

    public String msgData() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String msgData = request.getParameter("msgData");
        logger.info(msgData);
        String redisHosts = "";
        String dbName = "";
        String masterName = "";
        if (msgData.equals("yizhuang")) {
            redisHosts = PicCopy.readProperties("yz.redis.hosts");
            dbName = PicCopy.readProperties("yz.redis.db.name");
            masterName = PicCopy.readProperties("yz.redis.master.name");
            logger.info("亦庄redis" + redisHosts + ";" + dbName + masterName);
        } else {
            redisHosts = PicCopy.readProperties("redis.hosts");
            dbName = PicCopy.readProperties("redis.db.name");
            masterName = PicCopy.readProperties("redis.master.name");
        }
        String yesterdayDate = DateUtil.getYesterdayDate();

        RedisUtil userRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));

        String allCount_6 = userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(-6));
        String allCount_5 = userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(-5));
        String allCount_4 = userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(-4));
        String allCount_3 = userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(-3));
        String allCount_2 = userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(-2));
        String allCount_1 = userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(-1));
        String allCount_0 = userRedisUtil.get("twx_user_allcount_" + DateUtil.getToDayDate_1());
        logger.info("today data " + allCount_0);
        int todayAllCountIncr = 0;
        if (allCount_0 == null) {
            allCount_0 = "0";
        } else {
            todayAllCountIncr = (Integer.parseInt(allCount_0) - Integer.parseInt(allCount_1));
        }
        if (allCount_1 == null) {
            allCount_1 = "0";
        }
        if (allCount_2 == null) {
            allCount_2 = "0";
        }
        if (allCount_3 == null) {
            allCount_3 = "0";
        }
        if (allCount_4 == null) {
            allCount_4 = "0";
        }
        if (allCount_5 == null) {
            allCount_5 = "0";
        }
        if (allCount_6 == null) {
            allCount_6 = "0";
        }
        Object ob = JSON.toJSON("{\"categories\": [\"" + DateUtil.getDate(-6) + "\", \"" + DateUtil.getDate(-5) + "\", \"" + DateUtil.getDate(-4) + "\", \"" + DateUtil.getDate(-3) + "\", \"" + DateUtil.getDate(-2) + "\", \"" + DateUtil.getDate(-1) + "\", \"" + DateUtil.getToDayDate_1()
                + "\"],\"data\": ["
                + allCount_6 + ", " + allCount_5 + ", " + allCount_4 + ", " + allCount_3
                + ", " + allCount_2 + ", " + allCount_1 + ", " + allCount_0
                + "],\"incr\":[\"" + (Integer.parseInt(allCount_5) - Integer.parseInt(allCount_6))
                + "\",\"" + (Integer.parseInt(allCount_4) - Integer.parseInt(allCount_5))
                + "\",\"" + (Integer.parseInt(allCount_3) - Integer.parseInt(allCount_4))
                + "\",\"" + (Integer.parseInt(allCount_2) - Integer.parseInt(allCount_3))
                + "\",\"" + (Integer.parseInt(allCount_1) - Integer.parseInt(allCount_2)) + "\",\"" + (Integer.parseInt(allCount_5) - Integer.parseInt(allCount_6))
                + "\",\"" + todayAllCountIncr + "\"]}");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        out.print(ob);
        out.close();
        userRedisUtil.close();
        return "success";
    }

    public String monthData() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        String monthData = request.getParameter("monthData");
        logger.info(monthData);
        String redisHosts = "";
        String dbName = "";
        String masterName = "";
        if (monthData.equals("yizhuang")) {
            redisHosts = PicCopy.readProperties("yz.redis.hosts");
            dbName = PicCopy.readProperties("yz.redis.db.name");
            masterName = PicCopy.readProperties("yz.redis.master.name");
        } else {
            redisHosts = PicCopy.readProperties("redis.hosts");
            dbName = PicCopy.readProperties("redis.db.name");
            masterName = PicCopy.readProperties("redis.master.name");
        }
        RedisUtil userRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));
//        HashMap<String, String> hashMap = new HashMap<>();
        String categories = "{\"categories\": [\"";
        String allCount = "\"data\": [";
        String incr = "\"incr\":[\"";
        for (int i = -30; i < 1; i++) {
//            hashMap.put(DateUtil.getDate(i), userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(-i)));
            String twx_user_allcount = userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(i));
            String twx_user_allcount_1 = userRedisUtil.get("twx_user_allcount_" + DateUtil.getDate(i - 1));
            if (twx_user_allcount == null || twx_user_allcount_1 == null) {
                twx_user_allcount = "0";
                twx_user_allcount_1 = "0";
            }
            if (i < 0) {
                categories += DateUtil.getDate(i) + "\", \"";
                allCount += twx_user_allcount + ", ";
                incr += Integer.parseInt(twx_user_allcount) - Integer.parseInt(twx_user_allcount_1) + "\",\"";
            } else {
                categories += DateUtil.getDate(i) + "\"],";
                allCount += twx_user_allcount + "],";
                incr += Integer.parseInt(twx_user_allcount) - Integer.parseInt(twx_user_allcount_1) + "\"]}";
            }
        }

        Object ob = JSON.toJSON(categories + allCount + incr);
//        logger.info(categories + allCount + incr);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        out.print(ob);
        out.close();
        userRedisUtil.close();
        return "success";
    }

    public String msgHotData() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String msgData = request.getParameter("msgData");
        logger.info(msgData);
        String redisHosts = "";
        String dbName = "";
        String masterName = "";
        if (msgData.equals("yizhuang")) {
            redisHosts = PicCopy.readProperties("yz.redis.hosts");
            dbName = PicCopy.readProperties("yz.redis.db.name");
            masterName = PicCopy.readProperties("yz.redis.master.name");
            logger.info("亦庄redis" + redisHosts + ";" + dbName + masterName);
        } else {
            redisHosts = PicCopy.readProperties("redis.hosts");
            dbName = PicCopy.readProperties("redis.db.name");
            masterName = PicCopy.readProperties("redis.master.name");
        }
        RedisUtil userRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));

        String allCount_6 = userRedisUtil.get("twx_hotuser_count_" + DateUtil.getDate(-6));
        String allCount_5 = userRedisUtil.get("twx_hotuser_count_" + DateUtil.getDate(-5));
        String allCount_4 = userRedisUtil.get("twx_hotuser_count_" + DateUtil.getDate(-4));
        String allCount_3 = userRedisUtil.get("twx_hotuser_count_" + DateUtil.getDate(-3));
        String allCount_2 = userRedisUtil.get("twx_hotuser_count_" + DateUtil.getDate(-2));
        String allCount_1 = userRedisUtil.get("twx_hotuser_count_" + DateUtil.getDate(-1));
        String allCount_0 = userRedisUtil.get("twx_hotuser_count_" + DateUtil.getToDayDate_1());
        logger.info("today data " + allCount_0);
        int todayAllCountIncr = 0;
        if (allCount_1 == null) {
            allCount_1 = "0";
        }
        if (allCount_0 == null) {
            allCount_0 = "0";
        } else {
            todayAllCountIncr = (Integer.parseInt(allCount_0) - Integer.parseInt(allCount_1));
        }

        if (allCount_2 == null) {
            allCount_2 = "0";
        }
        if (allCount_3 == null) {
            allCount_3 = "0";
        }
        if (allCount_4 == null) {
            allCount_4 = "0";
        }
        if (allCount_5 == null) {
            allCount_5 = "0";
        }
        if (allCount_6 == null) {
            allCount_6 = "0";
        }
        Object ob = JSON.toJSON("{\"categories\": [\"" + DateUtil.getDate(-6) + "\", \"" + DateUtil.getDate(-5) + "\", \"" + DateUtil.getDate(-4) + "\", \"" + DateUtil.getDate(-3) + "\", \"" + DateUtil.getDate(-2) + "\", \"" + DateUtil.getDate(-1) + "\", \"" + DateUtil.getToDayDate_1()
                + "\"],\"data\": ["
                + allCount_6 + ", " + allCount_5 + ", " + allCount_4 + ", " + allCount_3
                + ", " + allCount_2 + ", " + allCount_1 + ", " + allCount_0
                + "],\"incr\":[\"" + (Integer.parseInt(allCount_5) - Integer.parseInt(allCount_6))
                + "\",\"" + (Integer.parseInt(allCount_4) - Integer.parseInt(allCount_5))
                + "\",\"" + (Integer.parseInt(allCount_3) - Integer.parseInt(allCount_4))
                + "\",\"" + (Integer.parseInt(allCount_2) - Integer.parseInt(allCount_3))
                + "\",\"" + (Integer.parseInt(allCount_1) - Integer.parseInt(allCount_2)) + "\",\"" + (Integer.parseInt(allCount_5) - Integer.parseInt(allCount_6))
                + "\",\"" + todayAllCountIncr + "\"]}");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        out.print(ob);
        out.close();
        userRedisUtil.close();
        return "success";
    }

    public String toWxUser() {
        HttpServletRequest request = ServletActionContext.getRequest();
        trunk = request.getParameter("trunk");
        logger.info(trunk);
        return "success";
    }

    public String getTrunk() {
        return trunk;
    }

    public void setTrunk(String trunk) {
        this.trunk = trunk;
    }

}
